package com.relytosoft.mizar.analyze.common.utils;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.relytosoft.dubhe.common.core.exception.ServiceException;

import java.util.Objects;

/**
 * 比特大陆OpenApi的工具类
 *
 * @Author SunLi
 * @Date: 2022/5/13 11:01
 * @Version 1.0
 */
public class BitmanRequestUtil {

    public static String handlerResponse(String result) {
        if (StrUtil.isEmpty(result)) {
            throw new ServiceException("Bitman接口返回的结果错误");
        }
        try {
            JSONObject jsonObject = JSON.parseObject(result);
            if (jsonObject.getInteger("Code") != 0) {
                throw new ServiceException(String.format("Bitman接口返回的结果错误:Code:%1$s,Msg:%2$s", jsonObject.getInteger("Code"), jsonObject.getString("Msg")));
            }
            Object data = jsonObject.get("Result");
            return Objects.isNull(data) ? null : data.toString();
        } catch (Exception ex) {
            throw new ServiceException(ex.getMessage());
        }
    }
}
